﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;

namespace DesignLife.Admin
{
    public partial class MenuManager : System.Web.UI.Page
    {
        static bool statusTrue;
        static bool statusFalse;
        SqlConnection con = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlHead head = (HtmlHead)Page.Header;
            HtmlMeta metasearch2 = new HtmlMeta();
            HtmlMeta metasearch1 = new HtmlMeta();
            metasearch1.Name = "mô tả";
            metasearch1.Content = "trang web cá nhân của tôi";
            head.Controls.Add(metasearch1);
            metasearch2.Name = "từ khóa";
            metasearch2.Content = "ASP.Net, C #, SQL";
            head.Controls.Add(metasearch2);
            if (!Page.IsPostBack)
            {
                ddlmenumanager_parrent.Items.Add(new ListItem("Select Parrent", "0"));
                ddlmenumanager_parrent.Items.Add(new ListItem("service", "1"));
                ddlmenumanager_parrent.Items.Add(new ListItem("introdution", "2"));
                statusTrue = true;
                statusFalse = false;
                UpdateData(statusTrue, statusFalse);
            }
            fcEnabled();
        }

        private void fcEnabled()
        {
            if (hifID.Value.Equals(""))
            {
                managerAdd.Enabled = true;
                managerChange.Enabled = false;
            }
            else {
                managerAdd.Enabled = false;
                managerChange.Enabled = true;
            }
        }

        private void UpdateData(bool status1, bool status2)
        {
            string sql = "select * from Categories";
            if (status1 != status2)
            {
                if (status1)
                    sql += " where Status = 'True'";
                else
                    sql += " where Status = 'False'";
            }

            DataTable tbcomment = new DataTable();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
            con.Open();

            SqlCommand com = new SqlCommand(sql, con);
            tbcomment.Load(com.ExecuteReader());
            manager_item.DataSource = tbcomment;
            manager_item.DataBind();
            cmRefesh();
            if (con != null)
                con.Close();
        }

        private void cmRefesh()
        {
            managermessage.Text = "";
            hifID.Value = "";
            txtmenumanager_categoryname.Text = "";
            txtmenumanager_link.Text = "";
            txtmenumanager_summary.Text = "";
            ckbmanager_status.Checked = false;
            ddlmenumanager_parrent.SelectedIndex = 0;
            fcEnabled();
        }

        protected void manager_item_ItemCommand(object source, DataListCommandEventArgs e)
        {
            hifID.Value = ((Label)e.Item.FindControl("lbmanager_Table_ID")).Text;
            txtmenumanager_categoryname.Text = ((Label)e.Item.FindControl("lbmanager_Table_CategoryName")).Text;
            txtmenumanager_link.Text = ((Label)e.Item.FindControl("lbmanager_Table_Link")).Text;
            string lbParrent = ((Label)e.Item.FindControl("lbmanager_Table_Parrent")).Text;
            int i = 0;
            foreach (ListItem parrentlb in ddlmenumanager_parrent.Items) {
                if (parrentlb.Text.Equals(lbParrent)) {
                    ddlmenumanager_parrent.SelectedIndex = Convert.ToInt32(parrentlb.Value);
                }
            }
            txtmenumanager_summary.Text = ((Label)e.Item.FindControl("lbmanager_Table_Summary")).Text;
            string status = ((Label)e.Item.FindControl("lbmanager_Table_status")).Text;
            if (status.Equals("True"))
                ckbmanager_status.Checked = true;
            else
                ckbmanager_status.Checked = false;
            fcEnabled();
            
        }

        protected void manager_True_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbox = sender as CheckBox;
            statusTrue = cbox.Checked;
            if (!statusFalse & !statusTrue)
            {
                (sender as CheckBox).Checked = true;
                statusTrue = true;
            }
            UpdateData(statusTrue, statusFalse);

        }

        protected void ckbmanager_False_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cbox = sender as CheckBox;
            statusFalse = cbox.Checked;
            if (!statusFalse & !statusTrue)
            {
                (sender as CheckBox).Checked = true;
                statusFalse = true;
            }
            UpdateData(statusTrue, statusFalse);
        }

        protected void managerAdd_Click(object sender, EventArgs e)
        {
            fcEnabled();
            if (hifID.Value.Equals(""))
            {
                if (txtmenumanager_categoryname.Text.Equals(""))
                    managermessage.Text = "Category Name Is Not Null";
                else if (txtmenumanager_link.Text.Equals(""))
                    managermessage.Text = "Link Is Not Null";
                else if (txtmenumanager_summary.Text.Equals(""))
                    managermessage.Text = "Summary Is Not Null";
                else
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert into Categories values(@categoryname,@link,@parrent,@summary,@status)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@categoryname", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@link", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@parrent", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@summary", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@status", SqlDbType.Bit);
                    cmd.Parameters[0].Value = txtmenumanager_categoryname.Text;
                    cmd.Parameters[1].Value = txtmenumanager_link.Text;
                    cmd.Parameters[2].Value = ddlmenumanager_parrent.SelectedItem.Text;
                    cmd.Parameters[3].Value = txtmenumanager_summary.Text;
                    if (ckbmanager_status.Checked)
                        cmd.Parameters[4].Value = "True";
                    else
                        cmd.Parameters[4].Value = "False";
                    int process = cmd.ExecuteNonQuery();
                    if (process > 0)
                    {
                        if (con != null)
                            con.Close();
                        UpdateData(statusTrue, statusFalse);
                        managermessage.Text = "Insert Success";
                    }
                    else
                        managermessage.Text = "Insert Failed";
                }
            }
            else
                managermessage.Text = "Only Change Data";

        }

        protected void managerCancel_Click(object sender, EventArgs e)
        {
            cmRefesh();
        }

        protected void managerChange_Click(object sender, EventArgs e)
        {
            if (!hifID.Value.Equals(""))
            {
                if (txtmenumanager_categoryname.Text.Equals(""))
                    managermessage.Text = "Category Name Is Not Null";
                else if (txtmenumanager_link.Text.Equals(""))
                    managermessage.Text = "Link Is Not Null";
                else if (txtmenumanager_summary.Text.Equals(""))
                    managermessage.Text = "Summary Is Not Null";
                else if (ddlmenumanager_parrent.SelectedIndex == 0)
                    managermessage.Text = "Select Parrent, Please!!!";
                else
                {

                    con.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Update Categories Set CategoryName = @categoryname, Link=@link, Parrent = @parrent, Summary = @summary, Status=@status where CategoryID = '" + hifID.Value + "'";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@categoryname", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@link", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@parrent", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@summary", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@status", SqlDbType.Bit);
                    cmd.Parameters[0].Value = txtmenumanager_categoryname.Text;
                    cmd.Parameters[1].Value = txtmenumanager_link.Text;
                    cmd.Parameters[2].Value = ddlmenumanager_parrent.SelectedItem.Text;
                    cmd.Parameters[3].Value = txtmenumanager_summary.Text;
                    if (ckbmanager_status.Checked)
                        cmd.Parameters[4].Value = "True";
                    else
                        cmd.Parameters[4].Value = "False";
                    int process = cmd.ExecuteNonQuery();
                    if (con != null)
                        con.Close();
                    if (process > 0)
                    {
                        managermessage.Text = "Change Success";
                        UpdateData(statusTrue, statusFalse);
                    }
                    else
                        managermessage.Text = "Change Failed";
                }
            }
        }
    }
}